package com.aaie.gru.interview.controller;


import com.aaie.gru.gruenum.UserType;
import com.aaie.gru.interview.entity.GruSysInterview;
import com.aaie.gru.interview.entity.GruSysInterviewDto;
import com.aaie.gru.interview.entity.GruSysInterviewVo;
import com.aaie.gru.interview.service.IGruSysInterviewService;
import com.aaie.gru.response.ResMessage;
import com.aaie.gru.user.entity.GruSysGuardian;
import com.aaie.gru.util.CommonUtil;
import com.aaie.gru.util.ConstantUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lvxing
 * @since 2019-08-09
 */
@Api(tags = "访谈模块")
@RestController
@RequestMapping("/interview")
public class GruSysInterviewController {

    @Autowired
    IGruSysInterviewService gruSysInterviewService;

    /**
     * 按条件获取访谈信息
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "分页获取审核列表", notes = "分页获取审核列表")
    @PostMapping("/getGruSysInterviewList")
    public String getGruSysInterviewList(@RequestBody GruSysInterviewDto param,HttpSession session) {
        try {
            // 查询结果
            String userIdStr = CommonUtil.getUser(session).getUserId();
            String userTypeStr = CommonUtil.getUser(session).getUserType();
            if(StringUtils.isNotEmpty(param.getInterviewGuardianId()) && userTypeStr.equals(UserType.GUARDIAN.getValue())){
                param.setInterviewGuardianId(userIdStr);
                param.setInterviewStuStatus(ConstantUtil.THEME_NEW_N);
            }
            IPage<GruSysInterviewVo> page = gruSysInterviewService.getGruSysInterviewVoList(param);
            return ResMessage.ok(page.getRecords(), param.getCurrent(), param.getSize(), page.getTotal());
        } catch (Exception e) {
            //e.printStackTrace();
            return ResMessage.error("/interview/getGruSysInterviewList报错", 500);
        }
    }

    /**
     * 添加访谈数据
     *
     * @param gruSysInterview
     * @return
     */
    @ApiOperation(value = "添加访谈数据", notes = "添加访谈数据")
    @PostMapping("/addGruSysInterview")
    public String addGruSysInterview(@RequestBody GruSysInterview gruSysInterview, HttpSession session) {
        try {
            if (StringUtils.isNotEmpty(gruSysInterview.getInterviewId())) {
                return ResMessage.error("新增的数据不能有主键id", 200);
            }
            // 访谈时间不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewDate())) {
                return ResMessage.error("访谈时间不能为空", 200);
            }
            // 学生不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewStuId())) {
                return ResMessage.error("学生不能为空", 200);
            }
            // 监护人不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewGuardianId())) {
                return ResMessage.error("监护人不能为空", 200);
            }
            // 预警时间不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewWarningDate())) {
                return ResMessage.error("预警时间不能为空", 200);
            }
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            gruSysInterview.setCreateName(CommonUtil.getUser(session).getUserName());
            gruSysInterview.setCreateTime(updateTime);
            gruSysInterview.setCreateTimeSort(updateTimeSort);
            gruSysInterview.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysInterview.setUpdateTime(updateTime);
            gruSysInterview.setUpdateTimeSort(updateTimeSort);
            boolean save = gruSysInterviewService.save(gruSysInterview);
            if (!save) {
                return ResMessage.error("数据插入失败", 200);
            }
            return ResMessage.ok(gruSysInterview);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/interview/addGruSysInterview报错", 500);
        }
    }

    /**
     * 更新访谈数据
     *
     * @param gruSysInterview
     * @return
     */
    @ApiOperation(value = "更新访谈数据", notes = "更新访谈数据")
    @PostMapping("/editGruSysInterview")
    public String editGruSysInterview(@RequestBody GruSysInterview gruSysInterview, HttpSession session) {
        GruSysGuardian result = null;
        try {
            if (StringUtils.isEmpty(gruSysInterview.getInterviewId())) {
                return ResMessage.error("更新的数据必须有主键id", 200);
            }
            // 访谈时间不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewDate())) {
                return ResMessage.error("访谈时间不能为空", 200);
            }
            // 学生不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewStuId())) {
                return ResMessage.error("学生不能为空", 200);
            }
            // 监护人不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewGuardianId())) {
                return ResMessage.error("监护人不能为空", 200);
            }
            // 预警时间不能为空
            if (StringUtils.isEmpty(gruSysInterview.getInterviewWarningDate())) {
                return ResMessage.error("预警时间不能为空", 200);
            }
            // 创建人只能取用户账号，不取用户名称了，因为用户名称不是同一个字段，取起来太麻烦
            gruSysInterview.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysInterview.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysInterview.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            boolean b = gruSysInterviewService.updateById(gruSysInterview);
            if (!b) {
                return ResMessage.error("数据更新失败", 200);
            }
            return ResMessage.ok(gruSysInterview);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/interview/editGruSysInterview报错", 500);
        }

    }

    @ApiOperation(value = "按id获取访谈数据", notes = "按id获取访谈数据")
    @GetMapping("/getGruSysInterview")
    public String getGruSysInterview(String interviewId) {
        try {
            if (StringUtils.isEmpty(interviewId)) {
                return ResMessage.error("获取数据必须有主键id", 200);
            }
            GruSysInterview gruSysInterview = gruSysInterviewService.getById(interviewId);
            if (gruSysInterview == null) {
                return ResMessage.error("获取失败", 200);
            }
            return ResMessage.ok(gruSysInterview);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/interview/getGruSysInterview报错", 500);
        }

    }

    @ApiOperation(value = "删除访谈数据", notes = "删除访谈数据")
    @PostMapping("/delInterview")
    public String delInterview(@RequestBody GruSysInterview gruSysInterview) {
        try {
            if (StringUtils.isEmpty(gruSysInterview.getInterviewId())) {
                return ResMessage.error("删除数据必须有主键id", 200);
            }
            // 物料删除
            boolean b = gruSysInterviewService.removeById(gruSysInterview.getInterviewId());
            if (b) {
                return ResMessage.ok("删除数据成功");
            }
            return ResMessage.error("删除数据失败", 200);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/interview/delInterview报错", 500);
        }

    }

}
